<?php

namespace Addons\StoreGis\Controller;
use Home\Controller\AddonsController;

class StoreGisController extends AddonsController{
    var $model_name = 'com_md';
    public function lists($model = null, $page = 0) {
        is_array ( $model ) || $model = $this->getModel ( $this->model_name );

        // 解析列表规则
        $list_data = $this->_list_grid ( $model );
        $fields = $list_data ['fields'];
        // 搜索条件
        $map = $this->_search_map ( $model, $fields );

        $list_data ['list_data'] = M ( $this->model_name )->where($map)->select ();
        $this->assign ( $list_data );

        $this->display ();
    }

    public function edit($model = null, $id = 0) {
        is_array ( $model ) || $model = $this->getModel ( $this->model_name );
        $Model = D ( parse_name ( get_table_name ( $model ['id'] ), 1 ) );
        $id || $id = I ( 'id' );
        if (IS_POST) {
            // 获取模型的字段信息
            $Model = $this->checkAttr ( $Model, $model ['id'] );
            if ($Model->create () && $Model->save ()) {
                $this->success ( '保存' . $model ['title'] . '成功！', U ( 'lists?model=' . $model ['name'] ) );
            } else {
                $this->error ( $Model->getError () );
            }
        } else {
            $fields = get_model_attribute ( $model ['id'] );
            // 获取数据
            $data = M ( get_table_name ( $model ['id'] ) )->where(array('id'=>$id))->find ();
            $data || $this->error ( '数据不存在！' );

            $token = get_token ();
            if (isset ( $data ['token'] ) && $token != $data ['token'] && defined ( 'ADDON_PUBLIC_PATH' )) {
                $this->error ( '非法访问！' );
            }

            $this->assign ( 'fields', $fields );
            $this->assign ( 'data', $data );
            $this->meta_title = '编辑' . $model ['com_zip_name'];

            $this->display ();
        }
    }
    public function add($model = null) {
        is_array ( $model ) || $model = $this->getModel ( $this->model_name );
        $Model = D ( parse_name ( get_table_name ( $model ['id'] ), 1 ) );

        if (IS_POST) {
            // 获取模型的字段信息
            $Model = $this->checkAttr ( $Model, $model ['id'] );
            if ($Model->create () && $id = $Model->add ()) {
                $this->success ( '添加' . $model ['title'] . '成功！', U ( 'lists?model=' . $model ['name'] ) );
            } else {
                $this->error ( $Model->getError () );
            }
        } else {
            // 要先填写appid
            $map ['token'] = get_token ();
            $info = M ( 'member_public' )->where ( $map )->find ();

            if (empty ( $info ['appid'] ) || empty ( $info ['secret'] )) {
                $this->error ( '请先配置appid和secret', U ( 'home/MemberPublic/edit', 'id=' . $info ['id'] ) );
            }
            $fields = get_model_attribute ( $model ['id'] );
            $this->assign ( 'fields', $fields );
            $this->meta_title = '新增' . $model ['storename'];
            $this->display ();
        }
    }
    public function del() {
        $model = $this->getModel ( $this->model_name );
        parent::common_del ( $model );
    }
    public function getShopJson(){
        $map['com_md_long'] = array('neq', '');
        $map['com_md_lat'] = array('neq', '');
        $shops = M($this->model_name)
            ->field('com_zip_name as name, com_md_add as address, com_md_tel as tel, com_md_long as longitude, com_md_lat as latitude')
            ->where($map)->select();
        $this->ajaxReturn($shops,'json');
    }

    public function show(){  //转到shopMap.html
        $position['long'] = I('long');
        $position['lat'] = I('lat');
        $position['label'] = I('label');
        $this->assign ( 'position', $position );
        $this->display('shopMap');
    }

    public function test(){
//        $datas = M('com_md')->select();
//        $log = array();
//        foreach($datas as $data){
//            $x = $data['com_md_long'];
//            $y = $data['com_md_lat'];
//            $res = JsonGet('http://api.map.baidu.com/ag/coord/convert?from=2&to=4&x=' . $x . '&y=' . $y);
//            $arr = json_decode($res);
//            $point['com_md_long'] = base64_decode($arr->x);
//            $point['com_md_lat'] = base64_decode($arr->y);
//            $save = M('com_md')->where(array('id'=>$data['id']))->save($point);
//            $log[] = $data['com_zip_name'] . '修改:' . $save;
//        }
//        $this->ajaxReturn($log);
    }
}
